var qq_shot_list = [];
function qq_shot_close() {
	qq_shot_list = [];
	$('#tmp').html('');
	$('#qqhost_box').hide();
}
function qq_shot_uploaded() {

	var x = document.all ? document.frames["hidden_iframe"].document : document
			.getElementById("hidden_iframe").contentDocument;
	var doc = x.body.innerHTML;
	if (doc == '')
		return;

	eval('doc = ' + doc);
	var str = editor.getContent();
	for ( var i in doc) {
		var img = new Image();
		img.src = doc[i];
		var _i = i;
		str = str.replace(_i, doc[i]);
	}
	$('#tmp').html('sucessful');

	editor.setContent(str);

	setTimeout(function() {
		qq_shot_close();
	}, 1500);
}

function reset_img(node) {
	// check is image;
	if (typeof (node) != 'object' || node.tagName == undefined
			|| node.tagName.toLowerCase() != 'img')
		return node;

	if (node.getAttribute('word_img') == undefined) {
		var src = node.getAttribute('src');
	} else {
		var src = node.getAttribute('word_img');
	}

	src = src.replace(/\%20/g, ' ').replace('file:///', '');

	// node.setAttribute('width', '200');
	// node.setAttribute('height', '200');
	node.setAttribute('src', src); // decodeURI(src)
	node.removeAttribute('word_img');
	node.removeAttribute('orgsrc');
	node.removeAttribute('style');

	qq_shot_list.push(src);
//	qq_shot_list.push(decodeURI(src));
//	qq_shot_list.push(decodeURI(src.replace(/\//g, '\\')));
}
function reset_qqshot(str) {
	var d = document.createElement("div");
	d.innerHTML = str;

	for ( var i1 in d.childNodes) {
		reset_img(d.childNodes[i1]);
		for ( var i2 in d.childNodes[i1].childNodes) {
			reset_img(d.childNodes[i1].childNodes[i2]);
			for ( var i3 in d.childNodes[i1].childNodes[i2].childNodes) {
				reset_img(d.childNodes[i1].childNodes[i2].childNodes[i3]);
			}
		}
	}

	if (qq_shot_list.length > 0 && confirm('are u sure?')) {
		var ic = 0;
		var html = [];
		html
				.push('<form id="tmp" action="up.php" method="post" enctype="multipart/form-data" target="hidden_iframe" >');
		for ( var ix in qq_shot_list) {
			html.push('<input type="text" attr="auto_select" name="f' + ic
					+ '_name" value="' + qq_shot_list[ix] + '" />');
			html.push('<input type="file" name="f' + ic + '" value="" />');
			html.push('<br>');
			ic++;
		}
		html.push('<input type="submit" value="upload" />');
		html.push('</form>');
		html = html.join('');
		$('#qqshot_frm').html(html);
		$('input[attr=auto_select]').mouseover(function() {
			$(this).select();
		});
		$('#qqhost_box')
				.css(
						{
							top : $('#content').offset().top
									+ ($('#content').height() - $('#qqhost_box')
											.height()) / 2,
							left : $('#content').offset().left
									+ ($('#content').width() - $('#qqhost_box')
											.width()) / 2
						}).show();
		;
	}
	return d.innerHTML;
}
baidu.editor.plugins['QQShot'] = function() {
	$('#qqhost_box').hide();
};

baidu.editor.ui.QQShot = function(editor, title) {
	title = 'QQ Screenshot';
	var ui = new baidu.editor.ui.Button({
		className : 'edui-for-qqshot',
		title : title,
		onclick : function() {
			var x = reset_qqshot(editor.getContent());
			editor.setContent(x);
		}
	});

	return ui;
};
